import 'package:intl/intl.dart';

extension CUDateUtilsIntExtensions on int {
  String format02() {
    return toString().padLeft(2, '0');
  }

  ///秒变成 HH:MM:SS
  ///"%02d:%02d:%02d"
  String formatHHMMSS({String separator = ":"}) {
    final hours = this ~/ 3600;
    final minutes = (this % 3600) ~/ 60;
    final seconds = this % 60;
    String _pad(int num) => num.toString().padLeft(2, '0');
    return [_pad(hours), _pad(minutes), _pad(seconds)].join(separator);
  }

  ///秒变成 HH:MM
  ///"%02d:%02d
  String formatHHMM({String separator = ":"}) {
    final hours = this ~/ 3600;
    final minutes = (this % 3600) ~/ 60;
    String _pad(int num) => num.toString().padLeft(2, '0');
    return [_pad(hours), _pad(minutes)].join(separator);
  }

  ///秒变成 MM:SS
  ///"%02d:%02d
  String formatMMSS({String separator = ":"}) {
    final minutes = this ~/ 60;
    final seconds = this % 60;
    String _pad(int num) => num.toString().padLeft(2, '0');
    return [_pad(minutes), _pad(seconds)].join(separator);
  }

  //
  //%02d
  String format0X({int x = 2}) {
    final one = this ~/ 3600;
    String _pad(int num) => num.toString().padLeft(x, '0');
    return _pad(one);
  }
}

int get currentTimeMillis {
  return DateTime.now().millisecondsSinceEpoch;
}

//DateUtils 与官方冲突
class DateXUtils {
  DateXUtils._();

  // 默认的时间格式
  static const String ymdHms = 'yyyy-MM-dd HH:mm:ss';
  static final DateFormat dateFormatYmdHms = DateFormat(ymdHms);
  static const String ymd = 'yyyy-MM-dd';
  static final DateFormat dateFormatYmd = DateFormat(ymd);
  static const String hms = 'HH:mm:ss';
  static final DateFormat dateFormatHms = DateFormat(hms);
  static const String ymdHmsSss = 'yyyy-MM-dd HH:mm:ss.SSS';
  static final DateFormat dateFormatYmdHmsSss = DateFormat(ymdHmsSss);

  static DateFormat getFormat(String format) {
    if (format == ymdHmsSss) {
      return dateFormatYmdHmsSss;
    } else if (format == ymdHms) {
      return dateFormatYmdHms;
    } else if (format == ymd) {
      return dateFormatYmd;
    } else if (format == hms) {
      return dateFormatHms;
    }
    return DateFormat(format);
  }

  /// 按照指定格式把时间转换成字符串，格式的写法类似yyyy-MM-dd HH:mm:ss.SSS
  ///
  /// @param calendar 时间
  /// @param format   格式
  /// @return 时间字符串
  static String getFormatTime({DateTime? date, String format = ymdHms}) {
    try {
      return getFormat(format).format(date ??= DateTime.now());
    } catch (e) {
      print(e.toString());
      return "";
    }
  }

  /// 时间戳转换成指定格式
  static String getFormatTimeByMillis(int millis, {String format = ymdHms}) {
    return getFormatTime(date: DateTime.fromMillisecondsSinceEpoch(millis), format: format);
  }
}

void main() {
  // 示例用法
  DateTime now = DateTime.now();
  String formattedTime = DateXUtils.getFormatTime(date: now);
  print(formattedTime); // 输出当前时间的格式化字符串

  print(600.formatHHMMSS());
}
